package com.mooc.yohoshop.ui.car;

import com.google.gson.Gson;
import com.mooc.yohoshop.ApiService;
import com.mooc.yohoshop.App;
import com.mooc.yohoshop.dao.DaoDBUtils;
import com.mooc.yohoshop.dao.OffLineCarDaoEntity;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.CarEntity;
import com.mooc.yohoshop.entity.OffLineCarEntity;
import com.mooc.yohoshop.entity.SuccessEntity;
import com.mooc.yohoshop.network.BaseRepository;
import com.mooc.yohoshop.network.HttpFactory;
import com.mooc.yohoshop.network.HttpType;

import org.greenrobot.greendao.DbUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class CarRepo extends BaseRepository {

    //购物车生成订单
    public Observable<BaseEntity>requestSubCar(List<HashMap<String,Object>>list){
        return convertObservable(HttpFactory.factory(HttpType.UPLOADTYPE).getRetrofit()
                .create(ApiService.class).requestCreateOrder(
                        RequestBody.create(MediaType.parse("application/json"),
                                new Gson().toJson(list))));
    }

    //购物车列表
    public Observable<BaseEntity> requestSelCar(HashMap<String,Object>map){
        if (App.getInstance().isLogin())
            return onLineSelCar(map);
        else
            return offLineSelCar();
    }
    //获取在线购物车列表
    private Observable<BaseEntity> onLineSelCar(HashMap<String,Object>map){
        return convertObservable(HttpFactory.factory(HttpType.UPLOADTYPE)
                .getRetrofit().create(ApiService.class).requestSelCars(createBody(map)));
    }
    //获取离线购物车列表
    private Observable<BaseEntity> offLineSelCar(){
        List<OffLineCarEntity> list = new OffLineCarDaoEntity().selectAll();
        List<CarEntity.Values> data = new ArrayList<>();
        CarEntity carEntity = new CarEntity();
        carEntity.status = "200";
        carEntity.message = "缓存数据获取成功";
        for (int i = 0;i < list.size();i ++){
            CarEntity.Values values= new CarEntity.Values();
            values.car_id = 0;
            values.user_id = 1;
            List<CarEntity.CarGoodsValues> car_goods = new ArrayList<>();
            CarEntity.CarGoodsValues values1 = new CarEntity.CarGoodsValues();
            values1.shop_color = list.get(i).shop_color;
            values1.shop_num = list.get(i).shop_num;
            values1.shop_name = list.get(i).shop_name;
            values1.shop_price = list.get(i).shop_price;
            values1.shop_select = list.get(i).shop_select;
            values1.shop_size = list.get(i).shop_size;
            values1.car_goods_id = list.get(i).getId();
            values1.car_id = 0;
            values1.goods_id = list.get(i).goods_id;
            values1.car_path = list.get(i).car_path;
            car_goods.add(values1);
            values.car_goods = car_goods;
            data.add(values);
        }
        carEntity.data = data;
        return convertObservable(Observable.just(carEntity));
    }
    //批量添加购物车
    public Observable<BaseEntity> requestAddCares(ArrayList<CarEntity.Values> list){
        return convertObservable(HttpFactory.factory(HttpType.UPLOADTYPE)
                .getRetrofit().create(ApiService.class).requestInsertCars(createBody(list)));
    }
    //删除购物车
    public Observable<BaseEntity> requestDelCares(ArrayList<CarEntity.Values> list){
        if (App.getInstance().isLogin())
            return onlineDelCares(list);
        else
            return offlineDelCares(list);
    }

    //删除在线购物车
    public Observable<BaseEntity> onlineDelCares(ArrayList<CarEntity.Values> list){
        return convertObservable(HttpFactory.factory(HttpType.UPLOADTYPE)
                .getRetrofit().create(ApiService.class).requestDelCar(createBody(list)));
    }
    //删除离线购物车
    public Observable<BaseEntity> offlineDelCares(ArrayList<CarEntity.Values> list){
        OffLineCarDaoEntity offLineCarDaoEntity= new OffLineCarDaoEntity();
        for(int i = 0;i < list.size();i ++){
            OffLineCarEntity offLineCarEntity = new OffLineCarEntity();
            offLineCarEntity.shop_select = list.get(i).car_goods.get(0).shop_select;
            offLineCarEntity.car_path = list.get(i).car_goods.get(0).car_path;
            offLineCarEntity.shop_size = list.get(i).car_goods.get(0).shop_size;
            offLineCarEntity.shop_price = list.get(i).car_goods.get(0).shop_price;
            offLineCarEntity.shop_color = list.get(i).car_goods.get(0).shop_color;
            offLineCarEntity.shop_num = list.get(i).car_goods.get(0).shop_num;
            offLineCarEntity.shop_name = list.get(i).car_goods.get(0).shop_name;
            offLineCarEntity.goods_id = list.get(i).car_goods.get(0).goods_id;
            offLineCarEntity.setId(list.get(i).car_goods.get(0).car_goods_id);
            offLineCarDaoEntity.delete(offLineCarEntity);
        }
        SuccessEntity successEntity = new SuccessEntity();
        successEntity.status = "200";
        successEntity.message = "离线购物车删除成功";
        return convertObservable(Observable.just(successEntity));
    }
    //修改购物车
    public Observable<BaseEntity> requestUpdateCar(HashMap<String,Object>map){
        return convertObservable(HttpFactory.factory(HttpType.UPLOADTYPE)
                .getRetrofit().create(ApiService.class).requestUpdateCar(createBody(map)));
    }
}
